﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlServerCe;

namespace CPTECH.Repository
{
    class LocalDB: BaseDB
    {
        private static LocalDB instance;

        private LocalDB() { }

        public static LocalDB getInstance()
        {
            if (instance == null)
            {
                instance = new LocalDB();
            }
            return instance;
        }

        //retorna todos os locais na base
        public ArrayList selectAllLocais()
        {
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select cd_fis_local, ds_local from TB_LOCAL;", this.conn);
                SqlCeDataReader rdr = command.ExecuteReader();
                ArrayList result = new ArrayList();

                while (rdr.Read())
                {
                    result.Add(rdr.GetString(0) + " = " + rdr.GetString(1));
                }

                return result;
            }
            finally
            {
                this.close();
            }
        
        }

        //checa se código filial já existe.
        public bool checkCodLocal(string codLocal)
        {
            bool result = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("select * from TB_LOCAL where cd_fis_local =@codLocal;", this.conn);
                command.Parameters.AddWithValue("@codLocal", codLocal);
                SqlCeDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = true;
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //inseri novo Local.
        public bool insertNewLocal(string os, string codigoFilial, string codLocal, string descLocal, string dtRegistro, byte enviado)
        {
            bool resposta = false;
            try
            {
                this.open();
                SqlCeCommand command = new SqlCeCommand("insert into TB_LOCAL values('" + os + "','" + codigoFilial + "', '" + codLocal + "','" + descLocal +
                                                        "','" + dtRegistro + "','" + enviado + "');", this.conn);
                command.ExecuteNonQuery();
                resposta = true;
            }
            catch (FormatException)
            {
                resposta = false;
            }
            finally
            {
                this.close();
            }
            return resposta;
        }
    }
}
